package com.magic.smartcity.view.adapters;

import android.content.Context;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import org.w3c.dom.Text;

/**
 * @description:
 * @create: Create by Magic on 2022/2/7.
 */
public class MyExpandableListAdapter extends BaseExpandableListAdapter {
    private static final String TAG = MyExpandableListAdapter.class.getSimpleName();

    class ViewHolder {
        TextView text;

        public ViewHolder(TextView text) {
            this.text = text;
        }
    }

    private String[] armType;
    private String[][] arms;
    private Context context;

    public MyExpandableListAdapter(String[] armType, String[][] arms, Context context) {
        this.armType = armType;
        this.arms = arms;
        this.context = context;
    }

    @Override
    public int getGroupCount() {
        return armType.length;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return arms.length;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return armType[groupPosition];
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return arms[groupPosition][childPosition];
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        LinearLayout linearLayout;
        ViewHolder viewHolder;
        if (convertView == null) {
            linearLayout = new LinearLayout(context);
            TextView text = getTextView();
            viewHolder = new ViewHolder(text);
            linearLayout.addView(text);
            linearLayout.setTag(viewHolder);
        } else {
            linearLayout = (LinearLayout) convertView;
            viewHolder = (ViewHolder) linearLayout.getTag();
        }
        viewHolder.text.setText(armType[groupPosition]);
        Log.d("TAG",":::::::"+armType[groupPosition]);
        return linearLayout;
    }

    private TextView getTextView() {
        TextView textView = new TextView(context);
        textView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        textView.setGravity(Gravity.CENTER);
        textView.setTextSize(20.0f);
        return textView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        TextView textView;
        if (convertView==null){
            textView = getTextView();
        }else {
            textView = (TextView) convertView;
        }
        textView.setText(arms[groupPosition][childPosition]);
        return textView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}
